package com.slt.concurrency.mashibing2019.juc.c_007_LockAndTools.exercise;

import java.util.concurrent.Exchanger;

public class Exchanger01 {
    static Exchanger<String> exchanger = new Exchanger();
    public static void main(String[] args) {
        new Thread(()->{
            String s = "s1";
            try {
                //交换过后的值 返回赋值给exchange
                String exchange = exchanger.exchange(s);
                System.out.println(Thread.currentThread().getName() +": " + exchange);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();

        new Thread(()->{
            String s = "s2";
            try {
                String exchange = exchanger.exchange(s);
                System.out.println(Thread.currentThread().getName() +": " + exchange);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t2").start();
    }
}
